package org.example.tip.service.Impl;

import org.example.tip.entity.Tip;
import org.example.tip.entity.vo.TipVO;
import org.example.tip.repository.TipRepository;
import org.example.tip.service.TipService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TipServiceImpl implements TipService {
    private final TipRepository tipRepository;

    public TipServiceImpl(TipRepository tipRepository) {
        this.tipRepository = tipRepository;
    }

    @Override
    @Cacheable(value = "TipCache", sync = true)
    public PageInfo<Tip> alltip(int pageNumber, int pageSize) {
        System.out.println(pageNumber+"         "+pageSize);
        PageHelper.startPage(pageNumber, pageSize);
        List<Tip> l = tipRepository.selectAllTip();
        return new PageInfo<>(l);
    }

    @Override
    @CacheEvict(value = "TipCache", allEntries = true, beforeInvocation = true)
    public void addtip(Tip t) {
        tipRepository.addTipText(t);
        tipRepository.addtip(t);
    }

    @Override
    @Cacheable(value = "TipCache", sync = true)
    public TipVO gettip(Integer id) {
        TipVO t = tipRepository.gettip(id);
        tipRepository.incrementViews(id);
        return t;
    }
    @Override
    @Cacheable(value = "TipCache", sync = true)
    public PageInfo<TipVO> getMyTip(String UserEmail,int PageNumber,int PageSize) {
        PageHelper.startPage(PageNumber, PageSize);
        List<TipVO> l = tipRepository.getMyTip(UserEmail);
        return new PageInfo<>(l);
    }
    @Override
    public List<Integer> getMyGoodTipId(String UserEmail) {
        return tipRepository.getMyGoodTipId(UserEmail);
    }
    @Override
    @CacheEvict(value = "TipCache", allEntries = true, beforeInvocation = true)
    public void userGoods(String UserEmail,Integer id) {
        tipRepository.userGoods(UserEmail, id);
        tipRepository.incrementGoods(id);
    }
    @Override
    @CacheEvict(value = "TipCache", allEntries = true, beforeInvocation = true)
    public void cancelUserGoods(String UserEmail,Integer id) {
        tipRepository.cancelUserGoods(UserEmail, id);
        tipRepository.cancelGoods(id);
    }
    @Override
    @Cacheable(value = "TipCache", sync = true)
    public PageInfo<TipVO> getMyGoodsTip(String UserEmail,int PageNumber,int PageSize) {
        PageHelper.startPage(PageNumber, PageSize);
        List<TipVO> l = tipRepository.getMyGoodsTip(UserEmail);
        return new PageInfo<>(l);
    }
    @Override
    @Cacheable(value = "TipCache", sync = true)
    public PageInfo<TipVO> searchTip(String all,int PageNumber,int PageSize) {
        PageHelper.startPage(PageNumber, PageSize);
        List<TipVO> l = tipRepository.searchTip(all);
        return new PageInfo<>(l);
    }
}
